<template>
  <div class="login">
    <div class="box">
      <h1>系统登录</h1>
      <el-form
        label-width="80px"
        size="small"
        :model="formObj"
        :rules="checkForm"
        ref="loginForm"
      >
        <el-form-item label="用户名：" prop="account">
          <el-input v-model="formObj.account">
            <i slot="prefix" class="iconfont icon-yonghufill"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input
            :type="flag ? 'text' : 'password'"
            v-model="formObj.password"
          >
            <i slot="prefix" class="iconfont icon-mima"></i>
            <i
              slot="suffix"
              :class="[
                'yanjing',
                'iconfont',
                flag
                  ? 'icon-yanjing_huaban1'
                  : 'icon-05wangluoanquan_biyanjing',
              ]"
              @click="flag = !flag"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="goLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 导入users api 文件中的login方法
import { api_login } from "../../api/users.js";
export default {
  data() {
    return {
      //定义表单的绑定数据
      formObj: {
        account: "",
        password: "",
      },
      // 定义表单验证的对象
      checkForm: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 12, message: "用户名长度在3-12位", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      // 定义眼睛切换的开关
      flag: false,
    };
  },
  methods: {
    goLogin() {
      this.$refs.loginForm.validate(async b => {
        // 调用登录的中间api
        let rst = await api_login(this.formObj);
        console.log(rst)
        
        
        // 解构 rst 中的code 和 msg
        let { code, msg, token } = rst;
        if (code == 0) {
          // 将token存入到本地
          this.$local.set("sell_token", token);
          this.$router.replace("/home");
        } else {
          // element-ui提供的提示框
          this.$message.error(msg);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: #2b3d49;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    border: 1px solid red;
    width: 300px;
    height: 300px;
    h1 {
      font-size: 30px;
      font-weight: 700;
      color: white;
      text-align: center;
      margin: 30px 0;
    }
    form {
      .el-form-item {
        /deep/.el-form-item__label {
          color: white;
        }
        /deep/.el-input__inner {
          background-color: #273743;
          color: white;
          //输入框的class是.el-input__inner
        }
        button {
          width: 100%;
        }
      }
    }
  }
}

.yanjing {
  font-size: 28px;
}
</style>